<template>
  <v-app>
    <v-app-bar app color="primary" dark>
      <div class="softphone">
        <div class="softphone-status">
          <div>
            <i :class="['kfont', iconClasses]" />{{ extensionMessage2 }}
          </div>
        </div>

        <div class="softphone-operator">
          <div
            class="item"
            :class="{on: isLogin && isReady}"
            @click="toggleReady"
          >
            <i
              class="kfont"
              :class="isReady ? 'icon-yiguanzhu' : 'icon-30offlineoval'"
            />
            <span>{{ isReady ? "就绪" : "未就绪" }}</span>
          </div>

          <div class="item" :class="{on: isCanAnswer}">
            <i class="kfont" :class="'icon-zaixianjieda'" />
            <span>接听</span>
          </div>
          <div class="item">
            <i class="kfont" :class="'icon-end-copy'" />
            <span>挂断</span>
          </div>
          <div
            class="item"
            :class="{on: isLogin && isReady}"
            @click="toggleBusy"
          >
            <!--  icon-time-->
            <i class="kfont" :class="'icon-30offlineoval'" />
            <span>{{ isFree ? '空闲' : '示忙' }}</span>
          </div>
          <div
            class="item"
            :class="{on: canCall}"
            @click="callPhone"
          >
            <i class="kfont icon-Call" />
            <span>拨打</span>
          </div>
          <div class="item on" @click="doCallout">
            <i class="kfont" :class="'icon-docallout'" />
            <span>{{ calloutstatus == 'docallout' ? '外呼' : calloutstatus == 'preview' ? '预览' : calloutstatus == 'incall' ? '呼叫' : '等待' }}</span>
          </div>
          <!--        <div class="item" @click="() => changeState('Ready')">-->
          <!--          <i :class="['kfont', 'icon-phone4']" />-->
          <!--          <span>保持</span>-->
          <!--        </div>-->
          <!--        <div class="item" @click="() => changeState('Ready')">-->
          <!--          <i class='kfont' :class="'icon-call_transfer'" />-->
          <!--          <span>转接</span>-->
          <!--        </div>-->
          <div class="item on" @click="toggleLogin">
            <i
              class="kfont"
              :class="isLogin ? 'icon-logout' : 'icon-loginiconphone'"
            />
            <span>{{ isLogin ? '登出' : '登入' }}</span>
          </div>
        </div>
      </div>
    </v-app-bar>

    <v-main>
      <v-container>
        <div
          v-show="isCallInput"
          class="softphone-call"
        >
          <v-text-field
            v-model="callNumber"
            placeholder="请输入"
            @keyup.enter="call"
          />
        </div>
      </v-container>
    </v-main>

    <v-dialog
      v-model="busyReasonVisible"
      width="500"
    >
      <v-card>
        <v-card-title class="text-h5 grey lighten-2">
          请选择示忙原因
        </v-card-title>

        <v-card-text>
          <v-radio-group
            v-model="selectedReason"
            row
          >
            <v-radio
              v-for="item in busyReason"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </v-radio-group>
        </v-card-text>

        <v-card-actions>
          <v-spacer />
          <v-btn
            color="primary"
            text
            @click="onReasonConfirm"
          >
            确定
          </v-btn>
        </v-card-actions>
      </v-card>
    </v-dialog>
  </v-app>
</template>

<script>
import JdWebCall from 'jdcall'

export default {
  name: "ExampleView",
  data() {
    return {
      isLogin: false,
      loginData: {},
      jdCall: null,

      state: 'NotReady',
      isFree: false,

      busyReasonVisible: false,
      busyReason: [
        {value: 'Dining', label: '吃饭'},
        {value: 'Meeting', label: '开会'},
        {value: 'Training', label: '培训'},
        {value: 'Report', label: '报告'},
      ],
      selectedReason: '',

      isCallInput: false,
      callNumber: '',
      canCall: false,
      isCanAnswer: false,
      calloutstatus: 'docallout'
    }
  },
  computed: {
    extensionMessage2() {
      if (this.isLogin) {
        return `分机${this.loginData.user} 登入成功`
      }
      return '分机未登录，连接失败'
    },
    iconClasses() {
      if (this.isLogin) return 'icon-xuanzhong'
      return 'icon-circle'
    },
    isReady() {
      return this.state === 'Ready'
    }
  },
  created() {
    const jdCall = JdWebCall.CreateCall({
      callserverurl: 'https://demo.zkefu.cn',
      error(msg) {
        console.error(msg)
      }
    })
    this.jdCall = jdCall;

    // 集成登录
    this.login();
    jdCall.events.EventLogin =  (data) => {
      if (data.Result) {
        console.log('登录成功：', data)
        this.isLogin = true;
        this.canCall = true;
        this.loginData = data.ResultMsg;
      } else {
        console.log('登录失败: ', data)
        this.isLogin = false;
      }
    }

    // 状态变化时触发
    this.jdCall.events.EventState = (data) => {
      console.log('切换状态：', data)
      if(data.Result) {
        this.state = data.ResultMsg.ToState;
        this.isFree = !data.ResultMsg.rest;
      }
    }

    // 监听退出事件
    this.jdCall.events.EventLogOut = (data) => {
      console.log('退出：', data)
      if(data.Result) {
        this.isLogin = false;
      }
    }
  },
  methods: {
    getSeatState() {
      // 主动获取坐席状态
      this.jdCall.events.EventGetState =  (data) => {
        if (data.Result) {
          console.log('获取坐席状态', data.ResultMsg)
          this.state = data.ResultMsg.State;
        }
      }
      this.jdCall.actions.getAgentState()
    },
    readyEvent() {
      this.jdCall.actions.selChange('Ready')
    },
    changeState(state) {
      this.jdCall.actions.selChange(state);
    },
    toggleReady() {
      this.changeState(this.isReady ? 'NotReady' : 'Ready')
    },
    toggleLogin() {
      if (!this.isLogin) {
        this.login();
      } else {
        this.jdCall.actions.logOut();
      }
    },
    login() {
      this.jdCall.actions.loginbytoken('1010', '', '', '123456', 'ukewo')
    },
    // 小休(Rest)与置忙(Busy)之间切换，1.5 SDK文档说明: 传入 Busy 和 Rest
    toggleBusy() {
      if (this.isFree) {
        this.busyReasonVisible = true;
      } else {
        this.jdCall.actions.selChange('Rest')
      }
    },
    onReasonConfirm() {
      this.busyReasonVisible = false;
      console.log(this.selectedReason)
      this.jdCall.actions.selChange(this.selectedReason)
    },
    callPhone() {
      if (this.canCall) {
        this.isCallInput = true;
      }
    },
    call() {
      // TODO: demo里只有单个号码的操作
      this.jdCall.actions.call(JSON.stringify({phone: this.callNumber}))
      this.isCallInput = false
      this.canCall = false
      this.callNumber = ''
    },
    doCallout() {
      if( this.calloutstatus == 'wait'){
        // softPhone.cancledocallout()
      }else if(this.calloutstatus == 'preview'){

        this.calloutstatus = 'outboundcall'
        this.docallouticon = 'icon-outboundcall'

        top.acceptCallOut = true
        // softPhone.outboundcall()
      } else {
        this.jdCall.docallout()
      }
    }
  }
};
</script>

<style lang="scss" socped>
.softphone {
  //z-index: 100;
  //position: fixed;
  //top: 0;
  //left: 0;
  width: 100%;
  //height: 60px;
  color: #fff;
  //background-color: #001529;
  display: flex;
  justify-content: center;

  .softphone-status {
    display: flex;
    align-items: center;
    justify-content: center;
    flex-direction: column;
    align-items: center;
    margin-right: 30px;
  }

  &-operator {
    display: flex;

    .item {
      margin: 0 15px;
      display: flex;
      flex-direction: column;
      justify-content: space-around;
      color: rgba(#fff, 0.6);
      text-align: center;
      font-size: 14px;
      cursor: not-allowed;

      &.on {
        color: #fff;
        cursor: pointer;

        &:hover {
          background-color: rgb(175, 174, 174);
        }
      }

      i {
        font-size: 24px;
      }
    }
  }

  .softphone-call {
    width: 200px;
    height: 50px;
    position: absolute;
    bottom: -60px;
    right: -90px;
  }
}
</style>
